package vn.candyapp.qlct.database;


import java.util.Date;
import java.util.List;
import vn.candyapp.qlct.database.model.AccountList;
import vn.candyapp.qlct.database.model.CategoryList;
import vn.candyapp.qlct.database.model.PaymentStatusList;
import vn.candyapp.qlct.database.model.ThongKeDS;
import vn.candyapp.qlct.database.model.Transaction;
import vn.candyapp.qlct.database.model.TransactionTypeList;
import vn.candyapp.qlct.item.KieuTKGiaoDich;
import vn.candyapp.qlct.utils.IOHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public abstract class SQLiteHelper extends SQLiteOpenHelper {
	
	private static final String LOG_TAG = "SQLiteHelper";
	protected Context context;
	
	protected static final int DATABASE_VERSION = 1;
	protected static final String DATABASE_NAME = "qlct.db";
	
	
	public SQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = IOHelper.readFileFromAsset(context, "createTable.sql");
		executeBatchSQL(db, sql);
	}
	

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql = IOHelper.readFileFromAsset(context, "dropTable.sql");
		executeBatchSQL(db, sql);
		onCreate(db);
	}
	
	public void executeBatchSQL(SQLiteDatabase db, String sql) {
		String[] sqlList = sql.trim().split(";");
		for(String _sql : sqlList) {
			_sql = _sql.trim();
			db.execSQL(_sql);
		}
	}
	
	public void createDatabase() {
		getWritableDatabase();
	}
	
	public abstract CategoryList getAllCategories();
	
	public abstract CategoryList getAllCategories(int transType);
	
	public abstract long addCategory(String name, int type);
	
	public abstract long addNewCategory(String name);
	
	public abstract void deleteCategory(int id);
	
	public abstract void deleteAccount(int id);
	
	public abstract TransactionTypeList getAllTransactionType();
	
	public abstract void deleteTransaction(Transaction tran);
	
	public abstract PaymentStatusList getPaymentStatus();
	
	public abstract AccountList getAccounts();
	
	public abstract long insertNewTransaction(ContentValues values);
	
	public abstract long addNewTransaction(Transaction trans);
	
	public abstract List<Transaction> getAllTransaction(Date date, KieuTKGiaoDich dType); 
	
	public abstract ThongKeDS[] getThongKe(Date d, KieuTKGiaoDich dType);
	
	
}
